package com.esun.payment_api.controller;

import com.esun.common.api.CommonPage;
import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.InvCancelDetailBean;
import com.esun.payment_api.dao.InvCancelFormDao;
import com.esun.payment_api.domain.InvCancelForm;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.dto.InvCancelFormQueryParam;
import com.esun.payment_api.dto.SubmitApprovalParam;
import com.esun.payment_api.service.InvCancelFormService;
import com.esun.payment_api.service.XcxInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.List;

import static com.esun.common.utils.JWTUtils.getLoginUser;

@RestController
@RequestMapping("/invCancel")
public class InvCancelFormController {

    @Resource
    InvCancelFormService invCancelFormService;

    @Resource
    XcxInfoService xcxInfoService;

    @ApiOperation("发票作废-根据Id返回发票作废详情")
    @GetMapping("/getInvCancelDetail")
    @ResponseBody
    public CommonResult getInvCancelDetail(Integer id) {
        if (BlankUtils.isBlank(id)) {
            CommonResult.failed("发票作废id不能为空");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取用户信息");
        }
        InvCancelDetailBean invCancelDetail = invCancelFormService.getInvCancelDetail(user.getId(), id);
        //详情页面是否可编辑
        if (user.getId().equals(invCancelDetail.getOperatorId())) {
            if ("1".equals(invCancelDetail.getFlowStatus()) || "3".equals(invCancelDetail.getFlowStatus())) {
                invCancelDetail.setEditableCancel(true);
            } else {
                invCancelDetail.setEditableCancel(false);
            }
        } else {
            invCancelDetail.setEditableCancel(false);
        }
        return CommonResult.success(invCancelDetail);
    }

    @ApiOperation("发票作废-根据id删除")
    @RequestMapping("/deleteById")
    public CommonResult deleteById(Integer id) {
        try {
            int count = invCancelFormService.deleteById(id);
            if (count > 0) {
                return CommonResult.success(count);
            } else {
                return CommonResult.failed("删除失败");
            }
        } catch (Exception e) {
            return CommonResult.failed("未获取到发票作废id");
        }
    }

    @ApiOperation("发票作废-根据id修改/保存")
    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(@RequestBody InvCancelForm record) {
        if (record == null) {
            return CommonResult.failed("请检查参数");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(record.getId())) {
            return CommonResult.failed("id不能为空");
        }
        record.setUpdateBy(user.getId());
        int count = invCancelFormService.update(record);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("发票作废-发票作废提交")
    @RequestMapping("/invCancelSubmitApproval")
    public CommonResult invCancelSubmitApproval(@RequestBody SubmitApprovalParam param) {
        if (param == null) {
            return CommonResult.failed("请检查输入参数");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        param.setCoId(user.getCoId());
        param.setUserId(user.getId());
        param.setDeptId(user.getDeptId());
        if (BlankUtils.isBlank(param.getUuid())) {
            return CommonResult.failed("UUID不能为空");
        }
        if (BlankUtils.isBlank(param.getInvId())) {
            return CommonResult.failed("发票作废id不能为空");
        } else {
            String businessType = invCancelFormService.getBusinessTypeByInvCancelId(param);
            param.setBusinessType(businessType);
            String check = invCancelFormService.checkInvCancelSubmit(param.getInvId());
            if ("1".equals(check)) {
                invCancelFormService.invCancelSubmitApproval(param);
                if ("1".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)61,param.getInvId());
                } else if ("2".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)162, param.getInvId());
                } else if ("3".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)222,param.getInvId());
                } else if ("4".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)202,param.getInvId());
                } else if ("5".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)242,param.getInvId());
                } else if ("6".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)244,param.getInvId());
                } else if ("7".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)246,param.getInvId());
                } else if ("8".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)248,param.getInvId());
                } else if ("9".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)262,param.getInvId());
                } else if ("10".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)402,param.getInvId());
                } else if ("11".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)404,param.getInvId());
                } else if ("12".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)422,param.getInvId());
                } else if ("13".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)424,param.getInvId());
                }
                return CommonResult.success(check);
            } else {
                return CommonResult.failed(check);
            }
        }
    }

    @ApiOperation("发票作废-发票作废被拒再次提交")
    @RequestMapping("/invCancelResubmitApproval")
    public CommonResult invCancelResubmitApproval(@RequestBody SubmitApprovalParam param) {
        if (param == null) {
            return CommonResult.failed("请检查输入参数");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        param.setCoId(user.getCoId());
        param.setUserId(user.getId());
        param.setDeptId(user.getDeptId());
        if (BlankUtils.isBlank(param.getUuid())) {
            return CommonResult.failed("UUID不能为空");
        }
        if (BlankUtils.isBlank(param.getInvId())) {
            return CommonResult.failed("发票作废id不能为空");
        } else {
            String businessType = invCancelFormService.getBusinessTypeByInvCancelId(param);
            param.setBusinessType(businessType);
            String check = invCancelFormService.checkInvCancelSubmit(param.getInvId());
            if ("1".equals(check)) {
                invCancelFormService.invCancelResubmitApproval(param);
                if ("1".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)61,param.getInvId());
                } else if ("2".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)162, param.getInvId());
                } else if ("3".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)222,param.getInvId());
                } else if ("4".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)202,param.getInvId());
                } else if ("5".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)242,param.getInvId());
                } else if ("6".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)244,param.getInvId());
                } else if ("7".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)246,param.getInvId());
                } else if ("8".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)248,param.getInvId());
                } else if ("9".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)262,param.getInvId());
                } else if ("10".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)402,param.getInvId());
                } else if ("11".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)404,param.getInvId());
                } else if ("12".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)422,param.getInvId());
                } else if ("13".equals(param.getBusinessType())) {
                    xcxInfoService.getFlowIdAndSend((short)424,param.getInvId());
                }
                return CommonResult.success(check);
            } else {
                return CommonResult.failed(check);
            }
        }
    }

    @ApiOperation("发票作废-补充信息")
    @PostMapping("/updateById")
    @ResponseBody
    public CommonResult updateById(@RequestBody InvCancelForm record) {
        if (record == null) {
            return CommonResult.failed("请检查参数");
        }
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(record.getId())) {
            return CommonResult.failed("发票作废id不能为空");
        }
        record.setUpdateBy(user.getId());
        int count = invCancelFormService.updateById(record);
        if (count > 0) {
            return CommonResult.success(count, "补充信息成功");
        }
        return CommonResult.failed();
    }

    @ApiOperation("发票作废-根据发票作废类型查询发票")
    @GetMapping("/getListByType")
    @ResponseBody
    public CommonResult<CommonPage<InvCancelFormDao>> getListByType(InvCancelFormQueryParam param,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        InvCancelFormQueryParam queryParam = new InvCancelFormQueryParam();
        queryParam.setCoId(param.getCoId());
        /*//筛选：部门名称
        if (!BlankUtils.isBlank(param.getDeptName())) {
            queryParam.setDeptName(param.getDeptName());
        }
        //筛选：审批节点
        if (!BlankUtils.isBlank(param.getNodeStr())) {
            queryParam.setNodeStr(param.getNodeStr());
        }
        //筛选：开始时间-结束时间
        if (!BlankUtils.isBlank(param.getStartTimeStr()) && !BlankUtils.isBlank(param.getEndTimeStr())){
            queryParam.setStartTimeStr(param.getStartTimeStr());
            queryParam.setEndTimeStr(param.getEndTimeStr());
        }*/
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(user.getCoId())) {
            return CommonResult.failed("登录人公司id为空");
        }
        if (BlankUtils.isBlank(user.getId())) {
            return CommonResult.failed("登录人id为空");
        }
        if (BlankUtils.isBlank(user.getDeptId())) {
            CommonResult.failed("登录人部门id为空");
        }
        queryParam.setCoId(user.getCoId());
        queryParam.setUserId(user.getId());
        queryParam.setDeptId(user.getDeptId());
        //前后端约定：0-6
        if (!BlankUtils.isBlank(param.getTabStr())) {
            queryParam.setTabStr(param.getTabStr());
            switch (param.getTabStr()) {
                case "0":
                    //全部
                    break;
                case "1":
                    //未提交
                    queryParam.setFlowStatus("1");
                    break;
                case "2":
                    //进行中
                    queryParam.setFlowStatus("2");
                    break;
                case "3":
                    //已通过
                    queryParam.setFlowStatus("4");
                    break;
                case "4":
                    //已拒绝
                    queryParam.setFlowStatus("3");
                    break;
                default:
                    System.out.println("输入参数有误！");
                    break;
            }
        }
        if (!BlankUtils.isBlank(param.getSearchStr())) {
            queryParam.setSearchStr(param.getSearchStr());
        }

        List<InvCancelFormDao> list = invCancelFormService.getListByType(queryParam, pageSize, pageNum);
        CommonPage<InvCancelFormDao> invCancelFormDaoCommonPage = CommonPage.restPage(list);
        return CommonResult.success(invCancelFormDaoCommonPage);
    }

}
